package xyz.csmcool.imchat.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import xyz.csmcool.imchat.entity.RoomsChats;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import xyz.csmcool.imutil.dto.LastMessageDto;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author csmcool
 * @since 2022-08-14
 */
@Mapper
public interface RoomsChatsMapper extends BaseMapper<RoomsChats> {

    /**
     * 获取房间的最后一条消息
     * @param ids
     * @return
     */
    @Select("<script>" +
            "SELECT room_id id,message FROM (SELECT room_id,message  FROM `sjim_rooms_chats` " +
            "WHERE room_id " +
            "<if test='ids.size() > 0' >" +
            "IN" +
            "</if>" +
            "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')' >" +
            "#{item}" +
            "</foreach>" +
            " ORDER BY create_time DESC) a GROUP BY room_id" +
            "</script>")
    List<LastMessageDto> getLastMessage(@Param("ids") List<Integer> ids);

    /**
     * selectroomId*
     * @param roomId
     * @return
     */
    @Select("SELECT * FROM (SELECT * " +
            "FROM sjim_rooms_chats " +
            "WHERE room_id = #{id} AND status = 1 ORDER BY id DESC LIMIT 10) a ORDER BY a.id")
    List<RoomsChats> selectFirstHistoryChat(@Param("id") Integer roomId);


    /**
     * 翻页查看历史记录
     * @param page
     * @param limit
     * @param roomId
     * @param id
     * @return
     */
    @Select("SELECT * FROM (SELECT * FROM `sjim_rooms_chats` " +
            "WHERE room_id = #{roomId} AND id < #{id} ORDER BY create_time DESC LIMIT #{page},#{limit}) a ORDER BY a.id")
    List<RoomsChats> selectHistoryChat(@Param("page") Long page,@Param("limit") Long limit,
                                       @Param("roomId") Integer roomId,@Param("id") Integer id);

    /**
     * 查找我的聊天记录
     * @param roomId
     * @return
     */
    @Select("SELECT COUNT(*) FROM sjim_rooms_chats WHERE room_id = #{roomId}")
    Integer selectHistoryChatsCount(@Param("roomId") Integer roomId);

    /**
     * 查找历史图片片*
     * @param page
     * @param limit
     * @param roomId
     * @return
     */
    @Select("SELECT message FROM `sjim_rooms_chats` " +
            "WHERE room_id = #{roomId} AND type=2 ORDER BY create_time DESC LIMIT #{page},#{limit}")
    List<String> selectHistoryImg(@Param("page") Long page,@Param("limit") Long limit,
                               @Param("roomId") Integer roomId);

    /**
     * 查找我的聊天记录
     * @param roomId
     * @param type*
     * @return
     */
    @Select("SELECT COUNT(*) FROM sjim_rooms_chats WHERE room_id = #{roomId} AND type=#{type}")
    Integer selectHistoryCount(@Param("roomId") Integer roomId,@Param("type") Integer type);

    /**
     * 查找历史图片片*
     * @param page
     * @param limit
     * @param roomId
     * @return
     */
    @Select("SELECT message FROM `sjim_rooms_chats` " +
            "WHERE room_id = #{roomId} AND type=5 ORDER BY create_time DESC LIMIT #{page},#{limit}")
    List<String> selectHistoryFile(@Param("page") Long page,@Param("limit") Long limit,
                                  @Param("roomId") Integer roomId);

    /**
     * 删除聊天记录*
     * @param roomId
     * @return
     */
    @Delete("DELETE sjim_rooms_chats FROM sjim_rooms_chats WHERE room_id = #{id}")
    Boolean deleteRoomChats(@Param("id") Integer roomId);
}
